Repository navigation
fix: resolve props defined only via React.FC<Props> / React component type annotations (fixes #387) - #1208
Open
DeviousCardi wants to merge 1 commit into
Open
fix: resolve props defined only via React.FC<Props> / React component type annotations (fixes #387)#1208DeviousCardi wants to merge 1 commit into
DeviousCardi wants to merge 1 commit into
Conversation
…omponent type annotations (fixes reactjs#387) Components whose props are only expressed through a React function component type annotation (e.g. `interface Props {...}` referenced by `const C: React.FC<Props> = () => ...`) now get their props extracted. Root cause: findAssignedVariableType only recognized TypeScript TSTypeReference annotations, so Flow-parsed sources (GenericTypeAnnotation) and several component shapes yielded no props at all. Changes: - getTypeFromReactComponent: recognize React.FC/FunctionComponent/VFC/ VoidFunctionComponent (plus deprecated SFC/StatelessComponent) in both TS (TSTypeReference) and Flow (GenericTypeAnnotation) annotations - resolve props when a named function is assigned to an annotated variable (`const C: React.FC<Props> = Inner`) - resolve props from the generic argument of `React.memo<Props>(...)` - unwrapBuiltinTSPropTypes: also unwrap Flow GenericTypeAnnotation wrappers (React.PropsWithChildren/PropsWithRef/PropsWithoutRef) Regression tests: unit tests in getTypeFromReactComponent-test and codeTypeHandler-test, plus integration fixtures component_44-47. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai> Signed-off-by: DeviousCardi <115358213+DeviousCardi@users.noreply.github.com>
🦋 Changeset detectedLatest commit: 88ebd83 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #387
Problem
When a component's props are only defined through a React function component type annotation — e.g.
— react-docgen extracted no props at all whenever the source was parsed with the Flow parser (the
GenericTypeAnnotationshape was not recognized), and also missed props forReact.memo<Props>(...)and for named functions assigned to annotated variables (const C: React.FC<Props> = Inner).Root cause
findAssignedVariableTypeinsrc/utils/getTypeFromReactComponent.tsonly handledTSTypeReferenceannotations with four hard-coded names, so:React.FC<Props>(GenericTypeAnnotation) → ignoredReact.SFC/React.StatelessComponent→ ignoredReact.memo<Props>(fn)generics → never consultedconst C: React.FC<Props> = Inner(component resolved to the referenced function) → no parent declarator → ignoredFix
getFunctionComponentPropsType()recognizesFunctionComponent,FC,VoidFunctionComponent,VFC(plus deprecatedSFC/StatelessComponent) in both TS (TSTypeReference) and Flow (GenericTypeAnnotation) annotations.findReferencedVariableType()scans forconst C: React.FC<Props> = <this component>when the inline declarator lookup fails.getMemoPropsType()reads the props from the generic type argument of a wrappingReact.memo<Props>(...)call (TS and Flow).unwrapBuiltinTSPropTypesnow also unwraps FlowReact.PropsWithChildren/PropsWithRef/PropsWithoutRefwrappers.Tests
getTypeFromReactComponent-test.ts(TS + Flow FC annotations, aliases, memo generics, assigned function reference, PropsWithChildren unwrap) with snapshots.codeTypeHandler-test.ts(props extraction from interfaces, incl.extendsheritage, for flow + ts + memo + assigned-reference).component_44.tsx(FC + interface extends),component_45.tsx(memo generics),component_46.tsx(assigned named function),component_47.js(flow FC + interface) with snapshots.Verification
npx vitest runinpackages/react-docgen: 69 files / 1513 tests passed (was 1494 before; +19 new).packages/react-docgen-cli: 57 tests passed (aftertscbuild).npx tsc --noEmitclean;eslint(prettier) clean.Written with assistance from Mistral Vibe (AI), reviewed and tested by me.